package wang.hu.bos.web.action.transit;

import java.util.HashMap;
import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;

import wang.hu.bos.domain.transit.TransitInfo;
import wang.hu.bos.service.transit.TransitService;
import wang.hu.bos.web.action.BaseAction;

import com.opensymphony.xwork2.ActionContext;

@Controller
@Namespace("/")
@ParentPackage("json-default")
@Scope("prototype")
public class TransitAction extends BaseAction<TransitInfo> {
	@Autowired
	private TransitService transitService;
	private String wayBillIds;

	public void setWayBillIds(String wayBillIds) {
		this.wayBillIds = wayBillIds;
	}

	@Action(value = "transit_save", results = { @Result(name = "success", type = "json") })
	public String save() {
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			transitService.save(wayBillIds);
			result.put("success", true);
			result.put("msg", "中转配送成功!");
		} catch (Exception e) {
			e.printStackTrace();
			result.put("success", false);
			result.put("msg", "中转配送失败!");
		}
		ActionContext.getContext().getValueStack().push(result);
		return SUCCESS;
	}

	@Action(value = "transit_pageData", results = { @Result(name = "success", type = "json") })
	public String pageData() {
		Pageable pageable = new PageRequest(page - 1, rows);
		Page<TransitInfo> pageData = transitService.pageData(pageable);
		pushPageableToValueStack(pageData);
		return SUCCESS;
	}

}
